package edu.gdin.ecommerce.dao;

import org.springframework.stereotype.Repository;

import edu.gdin.ecommerce.model.Account;
import edu.gdin.ecommerce.util.Pager;

@Repository("accountDAO")
public class AccountDAO extends BaseDAO {

	public Pager queryAccount(String cardNo, String accountName, Integer useType,
		 String customerName, String operTime1,
			String operTime2, Integer cPage) {
		
		String hql = "from Account a where 1=1";
		
		if((cardNo!=null && !cardNo.trim().equals("")) || (useType!=null && useType!=-1)){
			hql = "select c.account from Card c where 1=1";
			hql += (cardNo==null || cardNo.trim().equals("")) ? "" : " and c.cardNo="+cardNo;
			hql += (accountName==null || accountName.trim().equals("")) ? "" : " and c.account.name like '%"+accountName+"%'";
			hql += (customerName==null || customerName.trim().equals("")) ? "" : " and c.account.customer.name like '%"+customerName+"%'";
			hql += (useType==null || useType==-1) ? "" : " and c.useType ="+useType;
			hql += (operTime1==null || operTime1.trim().equals("")) ? "" : " and c.account.createTime>="+operTime2;
			hql += (operTime2==null || operTime2.trim().equals("")) ? "" : " and c.account.customer.name<="+operTime2;
	
		}else{
			
			hql += (accountName==null || accountName.trim().equals("")) ? "" : " and a.name like '%"+accountName+"%'";
			hql += (customerName==null || customerName.trim().equals("")) ? "" : " and a.customer.name like '%"+customerName+"%'";
			hql += (operTime1==null || operTime1.trim().equals("")) ? "" : " and a.createTime>="+operTime2;
			hql += (operTime2==null || operTime2.trim().equals("")) ? "" : " and a.customer.name<="+operTime2;
		}
		
		return this.getPage(hql, cPage);
	}

	public Account getAccountById(Integer accountId) {
		return (Account) this.findByPk(Account.class, accountId);
	}
	
	public Pager getAccountRecordById(Integer accountId, Integer cpg){
		String hql = "from AccountRecord ar where ar.account.accountId="+accountId;
		return this.getPage(hql, cpg);
	}
}
